import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ListSelectionModel;

/*
 * LoggedUsersGUI.java : This class is the shows the online/offline users
 *
 * @Author : BilCIS
 */

public class LoggedUsersGUI extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2347641005195033870L;

	// Variables declaration
	private JPanel contentPane;
	
	private DefaultListModel<String> dlm = null;
	private DefaultListModel<String> dlm2 = null;
	
	private JList<String> list;
	private JList<String> list2;
	
	private JButton refreshButton;
	
	private ButtonGroup	group;
	private JRadioButton	online;
	private JRadioButton	offline;
	
	private boolean track;
	
	private Employee emp;
	private Mysql mysql;

	// End of variables declaration
	
	public LoggedUsersGUI( Employee emp) throws SQLException
	{
		mysql = new Mysql(emp.getID());
		track = true;
		this.emp = emp;
		create();
		this.setVisible(true);
	}
	
	public void create() throws SQLException
	{
		//
		//Create controls
		//
		dlm = mysql.getOnline();
		dlm2 = mysql.getOffline();
		contentPane = new JPanel(null);
		refreshButton = new JButton("");
		group = new ButtonGroup();
		online = new JRadioButton("On", true);
		offline = new JRadioButton("Off");
		list = new JList<String> (dlm);
		list2 = new JList<String> (dlm2);
		//	
		//Register action listeners
		//		
		refreshButton.addActionListener( new refreshButton_Action() );
		on_off_Action groups = new on_off_Action();
		online.addActionListener(groups);
		offline.addActionListener(groups);
		list.addMouseListener(new mouseList() );
		list2.addMouseListener(new mouseList2() );
		//
		//Add controls to pane
		//
		this.add(contentPane);
		contentPane.add(refreshButton);
		contentPane.add(list);
		contentPane.add(list2);
		contentPane.add(online);
		contentPane.add(offline);
		group.add(online);
		group.add(offline);
		//
		//list
		//
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		list2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list2.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		//	
		//Set bounds
		//	
		contentPane.setBounds(0,0,150,250);
		refreshButton.setBounds(120,0,30,30);
		list.setBounds(0,35,150,220);
		list2.setBounds(0,35,150,220);
		online.setBounds(0,0,50,25);
		offline.setBounds(50,0,50,25);
		//	
		//	Setting Icons
		//
		refreshButton.setIcon(new ImageIcon( new File("refresh.gif").getAbsolutePath() ));
		//
		//	users
		//
		list.setForeground(new Color(0, 255, 50));
		list.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		list2.setVisible(false);
		list2.setForeground(new Color(255, 0, 0));
		list2.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		list.setModel(dlm);
		list2.setModel(dlm2);
		//
		// LoggesUsersGUI
		//	
		this.setSize(new Dimension(150, 250));
		this.setLayout(null);
	}
	
	// waits 2 click and opens mail for that user ( online )
	public class mouseList extends MouseAdapter
	{
	    public void mouseClicked(MouseEvent evt) 
	    {
	        evt.getSource();
	        if (evt.getClickCount() == 2) 
	        {        
	        	String mail = null;
	        	try {
					mail = mysql.getSpecificGmail((String) dlm.getElementAt(list.getSelectedIndex()));
				} catch (SQLException e) {
					e.printStackTrace();
				}
	        	new MailGUI(emp, mail, "", "");
	        }
	    }
	}
	
	// waits 2 click and opens mail for that user ( offline )
	public class mouseList2 extends MouseAdapter
	{
	    public void mouseClicked(MouseEvent evt) 
	    {
	        evt.getSource();
	        if (evt.getClickCount() == 2) 
	        {
	        	String mail = null;
	        	try {
					mail = mysql.getSpecificGmail((String) dlm2.getElementAt(list2.getSelectedIndex()));
				} catch (SQLException e) {
					e.printStackTrace();
				}
	        	new MailGUI(emp, mail, "", "");
	        }
	    }
	}

	// switches online-offline users
	public class on_off_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			Object source = e.getSource();
			if ( source == online )
			{
				track = true;
				try 
				{
					list2.setVisible(false);
					dlm = mysql.getOnline();
					list.setModel(dlm);
					list.setVisible(true);
				}
				catch (SQLException e1) 
				{
					e1.printStackTrace();
				}
			}
			else if ( source == offline )
			{
				track = false;	
				try 
				{
					list.setVisible(false);
					dlm2 = mysql.getOffline();
					list2.setModel(dlm2);
					list2.setVisible(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	// refresh the data
	public class refreshButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			Toolkit.getDefaultToolkit().beep();
			try
			{
				if ( track == true )
				{
					dlm = mysql.getOnline();
					list.setModel(dlm);
				}
				else
				{
					dlm2 = mysql.getOffline();
					list2.setModel(dlm2);
				}
			}
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			}
		}
	}
}